﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.BusinessAPI.ValidateUserAPI;
using System.Collections;
using TaobaoERP.BusinessTaobao.Common;
using TaobaoERP.Core.Common;
using TaobaoERP.BusinessTaobao.Core;
using System.Xml;
using System.Web;

namespace TaobaoERP.BusinessTaobao.ValidateUser
{
    public class ValidateUser: IValidateUser
    {
        /// <summary>
        /// 在调用任何其它接口前,必须要调用此接口.此接口用于验证用户身份，一般从Alisoft软件互联平台跳转到ISV应用的身份验证过程中需要使用到
        /// </summary>
        /// <param name="appUserId"></param>
        /// <param name="appInstanceId"></param>
        /// <param name="token"></param>
        /// <param name="Error"></param>
        /// <returns></returns>
        public bool validateUser(string appUserId, string appInstanceId, string token,string sessionid, out string Error)
        {
            string apiname = "alisoft.validateUser";
            Error = string.Empty;
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("appId", appid);
            mySL.Add("appInstanceId", appInstanceId);
            mySL.Add("userId", appUserId);
            mySL.Add("token", token);
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("sip_sessionid", sessionid);
            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString, out status);
            if (string.IsNullOrEmpty(status))
            {
                return false;
            }
            else
            {
                if ("9999".Equals(status))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(result);
                    XmlNode xn = xmlDoc.SelectSingleNode("String");
                    XmlElement xe = (XmlElement)xn;

                    return true;
                }
                else
                {
                    Error = Signature.GetMessage(result);
                    return false;
                }
            }
        }

        /// <summary>
        /// 根据阿里巴巴中文站（或淘宝）帐号，获取用户在软件互联平台的用户ID。
        /// </summary>
        /// <param name="LoginName"></param>
        /// <returns>
        /// <?xml version="1.0" encoding="utf-8" ?>
        ///    <StringResult>
        ///     <code>0</code>
        ///     <result>4007084</result>
        ///     </StringResult> 
        /// </returns>
        public string getUserId(string LoginName)
        {
            const string apiname = "alisoft.udb.getUserId";
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("domainid", 3);
            mySL.Add("loginid", LoginName);
            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString, out status);
            if (string.IsNullOrEmpty(status))
            {
                return "error";
            }
            else
            {
                if ("9999".Equals(status))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(result);
                    XmlNode xn = xmlDoc.SelectSingleNode("StringResult/result");
                    if (xn != null)
                    {
                        XmlElement xe = (XmlElement)xn;
                        return xe.InnerText;
                    }
                    else
                        return "未找到用户";
                }
                else
                {
                    return Signature.GetMessage(status);
                }
            }
        }

        /// <summary>
        /// 根据软件互联平台的用户ID，获取用户的阿里巴巴中文站（或淘宝）帐号。
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string getDomainLoginId(string userid)
        {
            string apiname= "alisoft.udb.getDomainLoginId";
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("domainid", 3);
            mySL.Add("userid", userid);
            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString,  out status);
            if (string.IsNullOrEmpty(status))
            {
                return "error";
            }
            else
            {
                if ("9999".Equals(status))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(result);
                    XmlNode xn = xmlDoc.SelectSingleNode("StringResult/result");
                    if (xn != null)
                    {
                        XmlElement xe = (XmlElement)xn;
                        return xe.InnerText;
                    }
                    else
                        return "未找到用户";
                }
                else
                {
                    return Signature.GetMessage(status);
                }
            }
        }

        /// <summary>
        /// 此接口方法以实现验证某个用户是否某个订阅实例的有效用户。
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="appInstanceId"></param>
        /// <returns>未测试</returns>
        public string validateAppUser(string userid, string appInstanceId)
        {
            string sip_apiname = "alisoft.validateAppUser";
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_apiname", sip_apiname);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("appId", appid);
            mySL.Add("appInstanceId", appInstanceId);
            mySL.Add("userId", userid);

            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString, out status);
            if (string.IsNullOrEmpty(status))
            {
                return "error";
            }
            else
            {
                if ("9999".Equals(status))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(result);
                    XmlNode xn = xmlDoc.SelectSingleNode("StringResult/result");
                    if (xn != null)
                    {
                        XmlElement xe = (XmlElement)xn;
                        return xe.InnerText;
                    }
                    else
                        return "未找到用户";
                }
                else
                {
                    return Signature.GetMessage(status);
                }
            }
        }
    }
}
